package learn.rxjava.train;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.subjects.PublishSubject;


public class LearnPublishSubject {
    public static void main(String[] args) {
        System.out.println("=====开始");
        LearnPublishSubject demo = new LearnPublishSubject();
        demo.test1();
        System.out.println("=====结束");
    }

    Observer observer1 = new Observer() {
        @Override
        public void onSubscribe(@NonNull Disposable d) {
//            System.out.println("onSubscribe observer1");
        }

        @Override
        public void onNext(@NonNull Object o) {
            System.out.println("onNext observer1===" + o);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            System.out.println("onError observer1===");
        }

        @Override
        public void onComplete() {
//            System.out.println("onComplete observer1");
        }
    };

    Observer observer2 = new Observer() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
//            System.out.println("onSubscribe observer2");
        }

        @Override
        public void onNext(@NonNull Object o) {
            System.out.println("onNext observer2===" + o);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            System.out.println("onError observer2===");
        }

        @Override
        public void onComplete() {
//            System.out.println("onComplete observer2");
        }
    };

    public void test1() {
        PublishSubject<Object> subject = PublishSubject.create();
        //
        subject.subscribe(observer1);

        subject.onNext("one");
        subject.onNext("two");

        subject.subscribe(observer2);

        subject.onNext("three");
        subject.onNext("four");
        subject.onComplete();
    }
}
